<?php
class Front_News_Model extends BaseModel{
	public function getForViewDetails($id){
		if(!isset($id) || $id == "")
			return null;
		$sqlUpdate = "UPDATE news set hit = hit + 1 where id='".$id."'";
		$row = DBHelper::getInstance()->execute($sqlUpdate);
		$sql = "SELECT p.id, p.name, p.details, p.image,
				p.newscat,
				c.name as ".News::CATNAME.",c.id as catid
				FROM news p
				LEFT JOIN newscat as c ON c.id = p.newscat
				WHERE p.id = '".$id."'";
		return DBHelper::getInstance()->fetchRow($sql);	
	}
	public function getDetailsForURL($id){
		$sql = "SELECT p.id,p.name,c.name as ".News::CATNAME."
				FROM news p
				LEFT JOIN newscat as c ON c.id = p.newscat
				WHERE p.id='".$id."'";
		return DBHelper::getInstance()->fetchRow($sql);
	}
	 public function getRelateCatById($context, $id){
		$sql = "SELECT p.id, p.name,p.image,c.name as ".News::CATNAME." 
				FROM news p
				LEFT JOIN newscat as c ON c.id = p.newscat
				WHERE p.newscat IN 
				( SELECT d.newscat from news d WHERE d.id = '".$id."')
						AND p.id != '".$id."'
				ORDER BY p.".News::CREATETIME." DESC
				 limit 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::newsDetails1($context, $value['id'], $value['name'], $value[News::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	public function getRelateCatByIdOrdinal($context, $id){
		$sql = "SELECT p.id, p.name,p.image,c.name as ".News::CATNAME."
				FROM news p
				LEFT JOIN newscat as c ON c.id = p.newscat
				WHERE p.newscat IN
				( SELECT d.newscat from news d WHERE d.id = '".$id."')
				ORDER BY p.".News::ORDINAL." DESC
				 limit 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::newsDetails1($context, $value['id'], $value['name'], $value[News::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	
	public function getRelateTopNews($context, $id){
		$sql = "SELECT p.id, p.name,p.image,c.name as ".News::CATNAME." 
				FROM news p
				LEFT JOIN newscat as c ON c.id = p.newscat
				WHERE p.id != '".$id."'
				ORDER BY p.".News::HIT." DESC
				 limit 0,8";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::newsDetails1($context, $value['id'], $value['name'], $value[News::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	}
	/*
	public function getAllForCatView($context, $catid){
		$sql = "SELECT p.id, p.name, p.image,p.price1, c.name as ".News::CATNAME."
				FROM News p
				LEFT JOIN Newscat as c ON c.id = p.Newscat
				WHERE p.Newscat = '".$catid."'
				ORDER BY p.Newsdisplay ASC
				LIMIT 0,15";
		$result = DBHelper::getInstance()->fetchAll($sql);
		if(isset($result) && count($result)>0){
			$rs = array();
			foreach ($result as $value) {
				$value['link'] = URLUltil::NewsDetails1($context, $value['id'], $value['name'], $value[News::CATNAME]);
				array_push($rs, $value);
			}
			$result = $rs;
		}
		return $result;
	} */
	public function getNewsCatName($catid){
		$sql = "SELECT name from newscat where id='".$catid."'";
		$result = DBHelper::getInstance()->fetchRow($sql);
		if(count($result)>0)
			return $result['name'];
		else return "";
	}
}